package com.ruoyi.mall.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.entity.MallMembers;
import com.ruoyi.mall.entity.Mask;
import com.ruoyi.mall.mapper.MallMembersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallSignMapper;
import com.ruoyi.mall.entity.MallSign;
import com.ruoyi.mall.service.IMallSignService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员签到Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-05-18
 */
@Service
public class MallSignServiceImpl implements IMallSignService 
{
    @Autowired
    private MallSignMapper mallSignMapper;
    @Autowired
    private MallMembersMapper mallMembersMapper;

    /**
     * 查询会员签到
     * 
     * @param signId 会员签到ID
     * @return 会员签到
     */
    @Override
    public MallSign selectMallSignById(Long signId)
    {
        return mallSignMapper.selectMallSignById(signId);
    }

    /**
     * 查询会员签到列表
     * 
     * @param mallSign 会员签到
     * @return 会员签到
     */
    @Override
    public List<MallSign> selectMallSignList(MallSign mallSign)
    {
        return mallSignMapper.selectMallSignList(mallSign);
    }

    /**
     * 新增会员签到
     * 
     * @param mallSign 会员签到
     * @return 结果
     */
    @Override
    public int insertMallSign(MallSign mallSign)
    {
        return mallSignMapper.insertMallSign(mallSign);
    }

    /**
     * 修改会员签到
     * 
     * @param mallSign 会员签到
     * @return 结果
     */
    @Override
    public int updateMallSign(MallSign mallSign)
    {
        return mallSignMapper.updateMallSign(mallSign);
    }

    /**
     * 删除会员签到对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMallSignByIds(String ids)
    {
        return mallSignMapper.deleteMallSignByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除会员签到信息
     * 
     * @param signId 会员签到ID
     * @return 结果
     */
    @Override
    public int deleteMallSignById(Long signId)
    {
        return mallSignMapper.deleteMallSignById(signId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult sign(MallSign mallSign, MallMembers mallMembers)   {
        List<MallSign> mallSigns = this.selectMallSignList(mallSign);
        List<Mask> masks = new ArrayList<>();
        //本月首次签到
        if (mallSigns.isEmpty()) {
            //添加积分
            BigDecimal integral = mallMembers.getIntegral();
            BigDecimal add_integral = integral.add(BigDecimal.ONE);
            mallMembers.setIntegral(add_integral);
            mallMembersMapper.updateMallMembers(mallMembers);
            //添加签到记录
            Mask mask = JSON.parseObject(mallSign.getMask(), Mask.class);
            masks.add(mask);
            mallSign.setSignMonth(DateUtils.getYyyyMm());
            mallSign.setMask(JSON.toJSONString(masks));
            mallSign.setTotalCounts(1L);
            mallSign.setContinueSigns(1L);
            this.insertMallSign(mallSign);

        } else {
            //本月非首次签到
            MallSign mallSigned = mallSigns.get(0);
            List<Mask> maskeds = JSON.parseArray(mallSigned.getMask(), Mask.class);
            return noContinueSign(maskeds,mallSigned, mallSign,mallMembers);
        }
        return null;
    }
    /**
     *
     *本月非首次签到
     * @param maskeds
     * @param mallSign
     */
    private AjaxResult noContinueSign(List<Mask> maskeds, MallSign mallSigned, MallSign mallSign,MallMembers mallMembers)   {
        //获取最后一个签到日期
        int size = maskeds.size();
        //签到数据
        Mask mask = maskeds.get(size - 1);
        Date maskDate = DateUtils.parseDate(mask.getDate());
        //当前签到数据
        Mask currentMask = JSON.parseObject(mallSign.getMask(), Mask.class);
        Date currentDate = DateUtils.parseDate(currentMask.getDate());
        if (DateUtils.isSameDay(currentDate, maskDate)) {
            return AjaxResult.error("今天已签到!");
        }
        long days = DateUtils.getDays(currentDate, maskDate);
        Long totalCounts = mallSigned.getTotalCounts();
        if(totalCounts==null){
            totalCounts = 0L;
        }
        ++totalCounts;
        //判断连续签到
        if (days == 1) {
            Long continueSigns = mallSigned.getContinueSigns();
            if(continueSigns==null){
                continueSigns = 0L;
            }
            ++continueSigns;
            if (continueSigns > 7) {
                continueSigns = 0L;
            }
            mallSign.setContinueSigns(continueSigns);
        }
        maskeds.add(currentMask);
        mallSigned.setMask(JSON.toJSONString(maskeds));
        mallSigned.setTotalCounts(totalCounts);
        //添加积分
        BigDecimal integral = mallMembers.getIntegral();
        BigDecimal add_integral = integral.add(BigDecimal.ONE);
        mallMembers.setIntegral(add_integral);
        mallMembersMapper.updateMallMembers(mallMembers);
        return AjaxResult.success(this.updateMallSign(mallSigned));
    }

    /**
     * 根据手机号查会员签到信息
     *
     * @param phone 手机号
     * @return 结果
     */
    @Override
    public List<MallSign> selectSignsByPhone(String phone,String signMonth) {
        return mallSignMapper.selectSignsByPhone(phone,signMonth);
    }
}
